package com.linko.intelligent.sys.core.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linko.intelligent.base.Node;
import com.linko.intelligent.common.util.TreeUtil;
import com.linko.intelligent.common.util.UserContext;
import com.linko.intelligent.sys.core.mapper.OrganizeMapper;
import com.linko.intelligent.sys.core.service.OrganizeService;
import com.linko.intelligent.sys.core.service.UserService;
import com.linko.intelligent.sys.pojo.dto.OrganizeDto;
import com.linko.intelligent.sys.pojo.po.Organize;
import com.linko.intelligent.sys.pojo.po.User;
import com.linko.intelligent.sys.pojo.query.OrganizeQuery;
import com.linko.intelligent.sys.pojo.query.UserQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author yuan
 * @create 2024/3/15 11:42
 */
@Service
public class OrganizeServiceImpl extends ServiceImpl<OrganizeMapper, Organize> implements OrganizeService {


    @Autowired
    private OrganizeMapper organizeMapper;

    @Autowired
    private UserService userService;

    @Override
    public Long save(OrganizeDto dto) {
        Organize po = new Organize();
        BeanUtils.copyProperties(dto, po);
        po.setCreatorId(UserContext.getUserId());
        po.setCreateTime(new Date());
        po.setDel(0L);
        save(po);
        return po.getId();
    }

    @Override
    public Boolean delete(Long id) {
        Organize po = new Organize();
        po.setId(id);
        po.setDel(1L);
        return updateById(po);
    }

    @Override
    public Boolean update(Long id, OrganizeDto dto) {
        Organize po = new Organize();
        BeanUtils.copyProperties(dto, po);
        po.setId(id);
        return updateById(po);
    }

    @Override
    public Organize getDetail(Long id) {
        Organize po = getById(id);
        return po;
    }

    @Override
    public List<Organize> queryList(OrganizeQuery query) {
        List<Organize> list = organizeMapper.selectLs(query);
        return list;
    }


    @Override
    public PageInfo<Organize> queryPage(OrganizeQuery query) {
        PageInfo<Organize> pageInfo = PageHelper.startPage(query.getPn(), query.getPs()).doSelectPageInfo(
                () -> queryList(query)
        );
        return pageInfo;
    }

    @Override
    public List<Node> getTreeById(Long id, boolean withUser) {
        List<Node> allNodes = new ArrayList<>();
        if (withUser) {
            allNodes = getAllNodeWithUser();
        } else {
            allNodes = getAllNode();
        }
        if (allNodes == null && allNodes.size() == 0) {
            return allNodes;
        }
        List<Node> trees = TreeUtil.buildListToTree(allNodes, false);
        List<Node> ls = new ArrayList<>();
        //查找目标id
        if (id != null) {
            for (Node tree : trees) {
                Node n = TreeUtil.queryNodeById(tree, "o:" + id);
                if (n != null) {
                    ls.add(n);
                }
            }
        } else {
            ls = trees;
        }
        //递归清理id
        if (ls != null && ls.size() > 0) {
            for (Node node : ls) {
                TreeUtil.clearId(node);
            }
        }
        return ls;
    }

    private List<Node> getAllNode() {
        List<Organize> organizes = queryList(new OrganizeQuery());
        List<Node> nodes = new ArrayList<>();
        for (Organize organize : organizes) {
            Node node = new Node();
            node.setId("o:" + organize.getId());
            node.setName(organize.getName());
            node.setParentId("o:" + organize.getPid());
            node.setType("" + organize.getType());
            nodes.add(node);
        }
        return nodes;
    }

    private List<Node> getAllNodeWithUser() {
        List<Organize> organizes = queryList(new OrganizeQuery());
        List<User> users = userService.queryList(new UserQuery());
        List<Node> nodes = new ArrayList<>();
        for (Organize organize : organizes) {
            Node node = new Node();
            node.setId("o:" + organize.getId());
            node.setName(organize.getName());
            node.setParentId("o:" + organize.getPid());
            node.setType("" + organize.getType());
            nodes.add(node);
        }
        for (User user : users) {
            Node node = new Node();
            node.setId("u:" + user.getId());
            node.setParentId("o:" + user.getOrganizeId());
            node.setName(user.getName());
            node.setType("" + (10));
            nodes.add(node);
        }
        return nodes;
    }

}
